package com.imooc.business.builder;

import com.imooc.business.facade.model.Organ;
import com.imooc.business.util.DateUtil;
import com.imooc.module.collect.enums.DimensionEnum;
import com.imooc.module.collect.enums.PeriodEnum;
import com.imooc.module.collect.request.ParamsRequest;
import com.imooc.module.collect.request.RequestPeriod;

import java.util.Date;
import java.util.Objects;

/**
 * @Author Tim
 * @Description:
 * @Version 1.0
 */
public class RequestBuilder {

    /**
     * 构建时间周期参数
     * @param periodEnum
     * @param collectDate
     * @return
     */
    public static RequestPeriod buildPeriod(PeriodEnum periodEnum, Date collectDate) {
        RequestPeriod requestPeriod = new RequestPeriod();
        requestPeriod.setPeriodEnum(periodEnum);
        requestPeriod.setCollectDate(collectDate);
        Date startTime = null;
        Date endTime = null;
        switch (periodEnum) {
            case DAY -> {
                startTime = DateUtil.getStartOfDay(collectDate);
                endTime = DateUtil.getEndOfDay(collectDate);
            }
            case WEEK -> {
                startTime = DateUtil.getStartOfWeek(collectDate);
                endTime = DateUtil.getEndOfWeek(collectDate);
            }
            case MONTH -> {
                startTime = DateUtil.getStartOfMonth(collectDate);
                endTime = DateUtil.getEndOfMonth(collectDate);
            }
            case SEMESTER -> {
                startTime = DateUtil.getStartOfSemester(collectDate);
                endTime = DateUtil.getEndOfSemester(collectDate);
            }
            case SCHOOL_YEAR -> {
                startTime = DateUtil.getStartOfSchoolYear(collectDate);
                endTime = DateUtil.getEndOfSchoolYear(collectDate);
            }
        }
        requestPeriod.setStartTime(startTime);
        requestPeriod.setEndTime(endTime);
        return requestPeriod;
    }

    /**
     * 组装 数据处理的最终参数
     * @param businessId
     * @param dimensionParameter
     * @param requestPeriod
     * @return
     */
    public static ParamsRequest buildParamsRequest(String businessId, ParamsRequest.DimensionParameter dimensionParameter, RequestPeriod requestPeriod) {
        ParamsRequest request = new ParamsRequest();
        request.setBusinessId(businessId);
        request.setDimensionParameter(dimensionParameter);
        request.setPeriodType(requestPeriod.getPeriodEnum().getName());
        request.setRequestDate(requestPeriod.getCollectDate());
        request.setStartTime(requestPeriod.getStartTime());
        request.setEndTime(requestPeriod.getEndTime());
        return request;
    }

    /**
     * 构建维度信息
     * @param organ
     * @param dimensionEnum
     * @return
     */
    public static ParamsRequest.DimensionParameter buildDimension(Organ organ, DimensionEnum dimensionEnum) {
        ParamsRequest.DimensionParameter dimensionParameter = new ParamsRequest.DimensionParameter();
        dimensionParameter.setDimension(dimensionEnum.getDimension());
        dimensionParameter.setSchoolId(organ.getSchoolId());
        dimensionParameter.setGroupId(organ.getGroupId());
        dimensionParameter.setUserIds(organ.getUserIds());
        return dimensionParameter;
    }

    /**
     * 构建下一个维度请求
     * @param request
     * @return
     */
    public static ParamsRequest buildNextParamsRequest(ParamsRequest request) {
        PeriodEnum period = PeriodEnum.parse(request.getPeriodType());
        DimensionEnum dimensionType = DimensionEnum.parse(request.getDimensionParameter().getDimension());

        return switch (period) {
            case CUSTOM -> null;
            case DAY, WEEK, MONTH, SEMESTER -> {
                //如果当前时间维度没有到 学年School_Year，就直接升级时间维度
                ParamsRequest.DimensionParameter dimension = buildDimension(request.getDimensionParameter(), dimensionType);
                RequestPeriod requestPeriod = buildPeriod(period.nextPeriod(), request.getRequestDate());
                yield buildParamsRequest(request.getBusinessId(), dimension, requestPeriod);
            }
            case SCHOOL_YEAR -> {
                //如果时间维度已经到了最后一个周期，接下来判断是否可以升级空间维度， 用户->群组->学校
                if (Objects.nonNull(dimensionType.nextDimension())){
                    ParamsRequest.DimensionParameter dimensionParameter = buildDimension(request.getDimensionParameter(), dimensionType.nextDimension());
                    RequestPeriod requestPeriod = buildPeriod(PeriodEnum.DAY, request.getRequestDate());
                    yield buildParamsRequest(request.getBusinessId(), dimensionParameter, requestPeriod);
                }
                yield null;
            }
        };
    }

    private static ParamsRequest.DimensionParameter buildDimension(ParamsRequest.DimensionParameter dimensionParameter, DimensionEnum dimensionType) {
        ParamsRequest.DimensionParameter dimension = new ParamsRequest.DimensionParameter();
        dimension.setDimension(dimensionType.getDimension());
        dimension.setUserIds(dimensionParameter.getUserIds());
        dimension.setGroupId(dimensionParameter.getGroupId());
        dimension.setSchoolId(dimensionParameter.getSchoolId());
        return dimension;
    }
}
